import Vue from 'vue'
import Router from 'vue-router'
import store from './store'
import Login from './Login.vue'
import Home from './Home.vue'
import Logon from './components/Logon.vue'
import Appointment from './components/Appointment.vue'
import Physical from './components/Physical.vue'
import Subscribe from './components/Subscribe.vue'
import Personal from './components/Personal.vue'
import Assessment from './components/Assessment.vue'
import Quetion from './components/Quetion.vue'
import Report from './components/Report.vue'
import Sculpture from './components/Sculpture.vue'
import Assess from './components/Assess.vue'
import Evaluate from './components/Evaluate.vue'
import Constitution from './components/Constitution'
import Physique from './components/Physique'
import Templist from './components/Templist'
import Dmission from './components/Dmission'
import Health from './components/Health'

Vue.use(Router)
const router = new Router({
  routes: [
    {path: '/login', name: 'login', component: Login},
    {path: '/home', component: Home},
    {path: '/home', name: 'home', component: Home},
    {path: '/logon', name: 'logon', component: Logon},
    {path: '/appointment', name: 'appointment', component: Appointment},
    {path: '/physical/:num', name: 'physical', component: Physical},
    {path: '/subscribe/:id/:num', name: 'subscribe', component: Subscribe},
    {path: '/personal', component: Personal},
    {path: '/assessment/:rid/:rval/:rname', component: Assessment},
    {path: '/quetion/:rid/:rval/:rname', component: Quetion},
    {path: '/report', component: Report},
    {path: '/sculpture/:besid', component: Sculpture},
    {path: '/assess', component: Assess},
    {path: '/templist', component: Templist},
    {path: '/evaluate/:rid/:rname', component: Evaluate},
    {path: '/constitution/:userid', component: Constitution},
    {path: '/physique/:userid', component: Physique},
    {path: '/dmission/:mid/:bid', component: Dmission},
    {path: '/health/:interId/:bid', component: Health},
    {path: '', name: 'login', redirect: '/login'}
  ]
})

export default router

router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    next()
  } else {
    // 如果访问的是路由其他组件，判断是否登录
    if (store.getters.isLog === false) {
      var user = JSON.parse(sessionStorage.getItem('user'))
      if (user != null) {
        store.commit('saveUser', user)
      }
    }
    if (store.getters.isLog) {
      next()
    } else {
      next()
    }
  }
})
